const db = wx.cloud.database()
const jj = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeList: "",
    inputValue: "",
    isSearch: false,
    isok: false,
    isFocus: false,
    Lable1: ['儿童', '抗疫志愿活动', '活动', '党', '环境', '青少年', '图书馆', '青春', '传递', '迎新'],
    oldLable: ['活动'],
    flag: [false, false, false, false, false, false, false, false, false, true],
  },
  oldLableSize: 10,
  TimeoutId: -1,
  addOldLable(str) {
    console.log("绑定传参的是", str);
    let value = str.currentTarget.dataset.str;
    console.log('添加的是', value)
    if (this.data.oldLable.length < this.oldLableSize) {
      let ans = this.data.oldLable;
      this.data.flag.splice(0, 1);
      let f = this.data.flag;
      f.push(true);
      ans.push(value);
      this.setData({
        oldLable: ans,
        flag: f,
      })
      console.log("添加后", this.data.oldLable)
    } else {
      this.data.oldLable.splice(0, 1);
      let ans = this.data.oldLable;
      ans.push(value);
      this.setData({
        oldLable: ans
      })
    }
    // this.oldLableId++;
    // console.log(this.data.oldLable)
  },
  inputText(str) {
    let value = str.currentTarget.dataset.str;
    console.log(value)
    this.setData({
      inputValue: value
    })
    this.GetData(value)
  },
  handleCancle() {
    this.setData({
      isFocus: false,
      inputValue: "",
      activeList: "",
    })
    this.onLoad()
  },
  handleInput(e) {
    console.log("e是", e)
    const val = e.detail.value;
    console.log("handInput第一次输出value=", val);
    if (!val) {
      this.setData({
        activeList: "",
        isFocus: false,
        isok: false,
      })
      return;
    }
    this.setData({
      isFocus: true,
      inputValue: val,
    })
    // console.log("this.input", this.data.inputValue)
    clearTimeout(this.TimeoutId);
    this.TimeoutId = setTimeout(() => {
      this.GetData(this.data.inputValue)
    }, 250)
  },
  GetData(str) {
    this.setData({
      isSearch: false,
      isok: false,
    })
    console.log("查询的是", str.trim())
    db.collection("activity").where({
      title: db.RegExp({
        regexp: str
      })
    }).get().then(res => {
      console.log("查询结果！", res)
      if (this.data.inputValue != "") {
        if (res.data.length != 0) {
          this.setData({
            activeList: res.data,
            isSearch: true,
          })
        } else {
          console.log('进入else')
          this.setData({
            isFocus: false,
          })
          if (this.data.inputValue != "") {
            console.log('inputValue不为空，isok要变为true')
            this.setData({
              isok: true,
            })
          } else {
            console.log('inputValue=', this.data.inputValue)
            this.setData({
              isok: false,
            })
          }
        }
      }
    }).catch(res => {
      console.log("请求失败")
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})